package com.dearbinge.data.service.impl.userWallet;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.userWallet.UserWalletService;
import com.dearbinge.data.dao.UserAuthenticationManage.UserAuthenticationManageMapper;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.UserWalletMapper;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.data.pojo.user.UserAuthenticationManage;
import com.dearbinge.data.pojo.userWallet.UserWallet;
import com.dearbinge.utils.PageUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "userWalletService")
public class UserWalletServiceImpl implements UserWalletService {

    @Autowired
    private UserWalletMapper userWalletMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAuthenticationManageMapper userAuthenticationManageMapper;

    @Override
    public Integer update(UserWallet userWallet) {
        Integer dataCode = userWalletMapper.update(userWallet);
        return dataCode;
    }

    @Override
    public Integer insert(UserWallet userWallet) {
        Integer dataCode = userWalletMapper.insert(userWallet);
        return dataCode;
    }

    @Override
    public UserWallet getById(Integer id) {
        UserWallet userWallet = userWalletMapper.getById(id);
        return userWallet;
    }

    @Override
    public Integer delete(Integer id) {
        Integer dataCode = userWalletMapper.delete(id);
        return dataCode;
    }

    @Override
    public Page getByPage(String userId, Integer deleted, Integer pageNo, Integer pageSize) {
        pageNo = PageUtils.getDefaultValues(pageNo, 1);
        pageSize = PageUtils.getDefaultValues(pageSize, 10);
        deleted = (null == deleted  ? 0 : deleted);
        Page<UserWallet> page = new Page<>(pageNo,pageSize);
        Integer count = userWalletMapper.count(userId,deleted);
        page.setTotal(count);
        List<UserWallet> list = userWalletMapper.getByPage(userId,deleted,page);
        page.setRecords(list);
        return page;
    }

    @Override
    public UserWallet getByUserId(String userId) {
        UserWallet userWallet = userWalletMapper.getByUserId(userId);
        return userWallet;
    }

    @Override
    public Map<String,Object> getByUserIdAnPhone(String userId) {
        Map<String,Object> mapList = new HashMap<String,Object>();
        Map<String,Object> map = new HashMap<String,Object>();
        UserWallet userWallet = userWalletMapper.getByUserId(userId);
        User user = userMapper.getById(Integer.parseInt(userId));
        UserAuthenticationManage userAuthenticationManageByUserId = userAuthenticationManageMapper.getUserAuthenticationManageByUserId(Integer.parseInt(userId));
        map.put("name",userAuthenticationManageByUserId == null ? null : userAuthenticationManageByUserId.getUserName());
        map.put("userWallet",userWallet);
        map.put("mobile", null);
        if (user.getMobile() != null) {
            map.put("mobile", user.getMobile());
        }
        mapList.put("userMsg",map);
        return mapList;
    }
}
